package com.sunshine.composeapp

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons.Filled
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sunshine.composeapp.bean.Message
import com.sunshine.composeapp.ui.theme.ComposeAppTheme

@ExperimentalMaterial3Api
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeAppTheme {
                // A surface container using the 'background' color from the theme
//                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
//                    Column(modifier = Modifier.fillMaxSize()) {
//                        Greeting(Message("Android"))
//                    }
//                }
//                MyApp(modifier = Modifier.fillMaxSize())
                MyApp()
            }
        }
    }

    @Composable
    fun Greeting(message: Message) {
        Row(modifier = Modifier.padding(all = 6.dp)) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "Image test", modifier = Modifier
                    .size(40.dp)
                    .clip(
                        CircleShape
                    )
            )
            Spacer(modifier = Modifier.width(8.dp))
            Column {
                Text(text = message.title)
                Spacer(modifier = Modifier.width(8.dp))
                Text(text = message.content)
            }
        }
    }

    @Composable
    fun Greeting(name: String) {
        var expanded by rememberSaveable {
            mutableStateOf(false)
        }
        val extraPadding by animateDpAsState(
            if (expanded) 48.dp else 0.dp
        )
        Card(
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.primary
            ), modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
        ) {
            Row(modifier = Modifier.padding(20.dp)) {
                Column(
                    modifier = Modifier
                        .weight(1f)
                        .padding(bottom = extraPadding)
                ) {
                    Text(
                        text = "hello,", style = MaterialTheme.typography.headlineMedium.copy(
                            fontWeight = FontWeight.ExtraBold
                        )
                    )
                    Text(text = name)
                    if (expanded) {
                        Text(
                            text = ("Compose ipsum color sit lazy, " + "padding theme it, sed do bouncy. ").repeat(4),
                        )
                    }
                }
                IconButton(onClick = {
                    expanded = !expanded
                }) {
                    Icon(
                        imageVector = if (expanded) Filled.KeyboardArrowUp else Filled.KeyboardArrowDown, contentDescription = if (expanded) {
                            stringResource(R.string.show_less)
                        } else {
                            stringResource(R.string.show_more)
                        }
                    )
                }
            }
        }
    }

    @Preview(name = "Light Mode")
    @Preview(uiMode = UI_MODE_NIGHT_YES, showBackground = true, name = "Dark Mode")
    @Composable
    fun DefaultPreview() {
        ComposeAppTheme {
            Surface {
                Greeting(Message("Android"))
            }
        }

    }

    @Composable
    fun Conversation(messages: List<Message>) {
        LazyColumn {
            items(messages) { message ->
                Greeting(message)
            }
        }
    }

    @Composable
    fun MyApp(
        modifier: Modifier = Modifier
    ) {
        var shouldShowOnboarding by rememberSaveable {
            mutableStateOf(true)
        }
//        Surface(modifier) {
        if (shouldShowOnboarding) {
            OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false }, modifier)
        } else {
            Greetings()
        }
//        }
    }

    @Composable
    private fun Greetings(
        modifier: Modifier = Modifier, names: List<String> = List(1000) { "$it" }
    ) {
        LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
            items(names) { name ->
                Greeting(name = name)
            }
        }
    }

    @Composable
    fun OnboardingScreen(onContinueClicked: () -> Unit, modifier: Modifier = Modifier) {
        Column(
            modifier = modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text("Welcome to the Basics CodeLab!")
            Button(
                onClick = onContinueClicked, modifier = Modifier
                    .padding(20.dp)
                    .clip(CircleShape)
            ) {
                Text("Continue")
            }
        }
    }

    @Preview(showBackground = true, widthDp = 1080, heightDp = 1920)
    @Composable
    fun OnboardingScreenPreview() {
        ComposeAppTheme {
            OnboardingScreen(onContinueClicked = {})
        }
    }

    @Preview
    @Composable
    fun MyAppPreview() {
        ComposeAppTheme {
            MyApp(Modifier.fillMaxSize())
        }
    }

    @Preview(
        showBackground = true, widthDp = 320, uiMode = UI_MODE_NIGHT_YES, name = "Dark"
    )
    @Preview(showBackground = true, widthDp = 320)
    @Composable
    fun DefaultGreetingsPreview() {
        ComposeAppTheme {
            Greetings(Modifier.fillMaxSize())
        }
    }
}